export default function updateNodeElement(newEl, virtualDOM, oldVirtualDOM = {}) {
    const props = virtualDOM.props || {}
    const oldProps = oldVirtualDOM.props || {}
    
    Object.keys(props).forEach(propName => {
        let val = props[propName]
        const oldVal = oldProps[propName]
        if (val === oldVal) {
            return
        }
        if (propName.startsWith('on')) {
            let eventName = propName.toLowerCase().slice(2)
            newEl.addEventListener(eventName, val)           
            if (oldVal) {
                newEl.removeEventListener(eventName, oldVal)
            }
        } else if (propName === 'value' || propName === 'checked') {
            newEl[propName] = val
        } else if (propName !== 'children') {
            if (propName === 'className') {
                newEl.setAttribute('class', val)
            } else {
                newEl.setAttribute(propName, val)
            }
        }
    })
    // 删除属性
    Object.keys(oldProps).forEach(propName => {
        const val = props[propName]
        const oldVal = oldProps[propName]
        if (!val) {
            if (propName.startsWith('on')) {
                newEl.removeEventListener(eventName, oldVal)
            } else if (propName === 'className') {
                newEl.removeAttribute('class')
            } else if (propName !== 'children') {
                newEl.removeAttribute(propName)
            }
        }
    })
}